package org.cpplib;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class PlanOptClassNew {
    public interface PLibrary extends Library {
        PLibrary INSTANCE = (PLibrary)Native.loadLibrary("..//dll//planopt", PLibrary.class);

        int schedule_file(String modelfile, int number, WORK_PIECE planeList[], WORK_PIECE tractorList,String planfile, String schfile, String preparefile,  int max_time);
        int schedule(String modelfile, int number, WORK_PIECE planeList[], WORK_PIECE tractorList,FLIGHT_PLAN flight_plan, String schfile, PLANE_INFO_LIST vJobInfos, PLANE_INFO_LIST vQYCinfos , int max_time);

    }

    private int schedule_file(String modelfile, int number, WORK_PIECE planeList[], WORK_PIECE tractorList,String planfile, String schfile, String preparefile,  int max_time)
    {
        return PLibrary.INSTANCE.schedule_file(modelfile,number,planeList,tractorList,planfile,schfile,preparefile,max_time);
    }

    private int schedule(String modelfile, int number, WORK_PIECE planeList[], WORK_PIECE tractorList,FLIGHT_PLAN flight_plan, String schfile, PLANE_INFO_LIST vJobInfos, PLANE_INFO_LIST vQYCinfos , int max_time)
    {
        return PLibrary.INSTANCE.schedule(modelfile,number,planeList,tractorList,flight_plan,schfile,vJobInfos, vQYCinfos,max_time);
    }

    private void test_schedule_file_ok()
    {
        String modelfile = "..//data//data.xml";
        String planfile = "..//data//plan.txt";
        String schfile = "";
        String preparefile = "prepare1.txt";
        int max_time = 5;

        WORK_PIECE[] planeList = (WORK_PIECE[]) new WORK_PIECE().toArray(3);

        planeList[0].job_type = UtilClass.getFixLenBytes("O_type1", 32);
        planeList[0].job_name = UtilClass.getFixLenBytes( "O11,O12,O13,O14,O15,O16,O17,O18",1024);

        planeList[1].job_type = UtilClass.getFixLenBytes("O_type2", 32);
        planeList[1].job_name = UtilClass.getFixLenBytes( "O21,O22,O23,O24,O25,O26,O27,O28",1024);

        planeList[2].job_type = UtilClass.getFixLenBytes("O_type3", 32);
        planeList[2].job_name = UtilClass.getFixLenBytes( "O31,O32,O33,O34",1024);

        WORK_PIECE tractorList = new WORK_PIECE();
        tractorList.job_name = UtilClass.getFixLenBytes("T11,T12,T13,T14,T15,T16",1024);

        System.out.println(schedule_file(modelfile, 3, planeList, tractorList, planfile, schfile, preparefile, max_time));

    }

    private void test_schedule_ok()
    {
        String modelfile = "..//data//data.xml";
        String schfile = "";
        int max_time = 5;

        WORK_PIECE[] planeList = (WORK_PIECE[]) new WORK_PIECE().toArray(3);

        planeList[0].job_type = UtilClass.getFixLenBytes("O_type1", 32);
        planeList[0].job_name = UtilClass.getFixLenBytes( "O11,O12,O13,O14,O15,O16,O17,O18",1024);

        planeList[1].job_type = UtilClass.getFixLenBytes("O_type2", 32);
        planeList[1].job_name = UtilClass.getFixLenBytes( "O21,O22,O23,O24,O25,O26,O27,O28",1024);

        planeList[2].job_type = UtilClass.getFixLenBytes("O_type3", 32);
        planeList[2].job_name = UtilClass.getFixLenBytes( "O31,O32,O33,O34",1024);

        WORK_PIECE tractorList = new WORK_PIECE();
        tractorList.job_name = UtilClass.getFixLenBytes("T11,T12,T13,T14,T15,T16",1024);

        FLIGHT_PLAN flight_plan = new FLIGHT_PLAN();

        flight_plan.id = 1;
        flight_plan.taskNumber = 4;

        flight_plan.jobTasks[0].job_type_name = UtilClass.getFixLenBytes("O_type1",32);
        flight_plan.jobTasks[0].job_name = UtilClass.getFixLenBytes("O11", 32);
        flight_plan.jobTasks[0].final_station = UtilClass.getFixLenBytes("S_type31", 32);
        flight_plan.jobTasks[0].init_station = UtilClass.getFixLenBytes("",32);
        flight_plan.jobTasks[0].batch = 1;
        flight_plan.jobTasks[0].startTime = UtilClass.getFixLenBytes("2022/06/30 08:00:00",32);
        flight_plan.jobTasks[0].endTime = UtilClass.getFixLenBytes("2022/06/30 18:00:00",32);

        flight_plan.jobTasks[1].job_type_name = UtilClass.getFixLenBytes("O_type1",32);
        flight_plan.jobTasks[1].job_name = UtilClass.getFixLenBytes("O12", 32);
        flight_plan.jobTasks[1].final_station = UtilClass.getFixLenBytes("S_type31", 32);
        flight_plan.jobTasks[1].init_station = UtilClass.getFixLenBytes("",32);
        flight_plan.jobTasks[1].batch = 1;
        flight_plan.jobTasks[1].startTime = UtilClass.getFixLenBytes("2022/06/30 08:15:00",32);
        flight_plan.jobTasks[1].endTime = UtilClass.getFixLenBytes("2022/06/30 18:15:00",32);

        flight_plan.jobTasks[2].job_type_name = UtilClass.getFixLenBytes("O_type1",32);
        flight_plan.jobTasks[2].job_name = UtilClass.getFixLenBytes("O13", 32);
        flight_plan.jobTasks[2].final_station = UtilClass.getFixLenBytes("S_type31", 32);
        flight_plan.jobTasks[2].init_station = UtilClass.getFixLenBytes("",32);
        flight_plan.jobTasks[2].batch = 2;
        flight_plan.jobTasks[2].startTime = UtilClass.getFixLenBytes("2022/06/30 09:00:00",32);
        flight_plan.jobTasks[2].endTime = UtilClass.getFixLenBytes("2022/06/30 19:00:00",32);

        flight_plan.jobTasks[3].job_type_name = UtilClass.getFixLenBytes("O_type1",32);
        flight_plan.jobTasks[3].job_name = UtilClass.getFixLenBytes("O14", 32);
        flight_plan.jobTasks[3].final_station = UtilClass.getFixLenBytes("S_type31", 32);
        flight_plan.jobTasks[3].init_station = UtilClass.getFixLenBytes("",32);
        flight_plan.jobTasks[3].batch = 2;
        flight_plan.jobTasks[3].startTime = UtilClass.getFixLenBytes("2022/06/30 09:15:00",32);
        flight_plan.jobTasks[3].endTime = UtilClass.getFixLenBytes("2022/06/30 19:15:00",32);

        PLANE_INFO_LIST vJobInfos = new PLANE_INFO_LIST();
        PLANE_INFO_LIST vQYCinfos = new PLANE_INFO_LIST();

        System.out.println(schedule(modelfile,3,planeList,tractorList,flight_plan,schfile,vJobInfos, vQYCinfos, max_time));

        String name, station;
        for (int i = 0; i < vJobInfos.number; i++) {
            name = new String(vJobInfos.pINfo[i].job_name);
            station = new String(vJobInfos.pINfo[i].init_station);
            System.out.println(name);
            System.out.println(station);
        }

        for (int i = 0; i < vQYCinfos.number; i++) {
            name = new String(vQYCinfos.pINfo[i].job_name);
            System.out.println(name);
        }

    }
    public void testAll()
    {
//        test_schedule_file_ok();

        test_schedule_ok();
    }

}
